package com.zy.controller;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zy.entity.Email;
import com.zy.entity.Post;
import com.zy.entity.User;
import com.zy.mapper.EmailMapper;
import com.zy.mapper.PostMapper;
import com.zy.mapper.UserMapper;
import com.zy.mapper.ZyDepartMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
public class ZyAppUserController {
    @Autowired
    public ZyDepartMapper zyDepartMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    EmailMapper emailMapper;
    @Autowired
    PostMapper postMapper;

    /**
     * @Author: 杨深俊
     * @Method: getAppUsers
     * @Date: 2021/4/7
     * @Time: 11:44
     * pageNum 当前页数
     * pageSize 每页记录条数
     */
    @RequestMapping("/appUserManage")
    public ModelAndView getAppUsers(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                    @RequestParam(value = "pageSize", defaultValue = "7") Integer pageSize,
                                    @RequestParam(value = "queryName", required = false) String queryName,
                                    @RequestParam(value = "queryPhone", required = false) String queryPhone,
                                    @RequestParam(value = "departmentName", required = false) String departmentName) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("AppUserManage");

        QueryWrapper queryWrapper = new QueryWrapper();
        if (!StringUtils.isEmpty(queryName)) {
            queryWrapper.like("name", queryName);
        } else {
            queryName = "";
        }
        if (!StringUtils.isEmpty(queryPhone)) {
            queryWrapper.like("phone", queryPhone);
        } else {
            queryPhone = "";
        }
        if (!StringUtils.isEmpty(departmentName)) {
            queryWrapper.like("post", departmentName);
        } else {
            departmentName = "";
        }
        //分页
        IPage<User> userIPage = new Page<>(pageNum, pageSize);
        IPage<User> users = userMapper.selectPage(userIPage, queryWrapper);
        List<Post> postList = postMapper.selectList(null);
        modelAndView.addObject("users", users);
        modelAndView.addObject("postList", postList);

        modelAndView.addObject("queryName", queryName);
        modelAndView.addObject("queryPhone", queryPhone);
        modelAndView.addObject("departmentName", departmentName);

        return modelAndView;
    }

    //给用户发邮件
    @PostMapping("addEmail")
    public String addEmail(@ModelAttribute Email email) {
        emailMapper.addEmail(email);
        return "redirect:/appEmailManage";
    }

    @GetMapping("getZyAppUser")
    @ResponseBody
    public User getUser(Integer id) {
        User user = userMapper.selectById(id);
        return user;
    }
}
